#coding:utf-8
'''This module contains all the operations dealing with process in which
   we extract the content of XML documents and reorganize it in a way
   the Stanford Segmenter and POStagger can further work with it so that
   we can reconstruct the XML structure easier.
'''

import xml.sax
from general.generaltools import SystemTools
from general.generaltools import Debugger
from objects.handler.documenthandler import DocContentHandler

def seg_and_pos_docs(**argv):
    error_record = {}
    debugger = Debugger(argv['debug'])
    '''
    result = parse(argv['web_doc'], 'web', 'web', isDebug=argv['debug'])
    error_record['parse_web'] = result

    result = parse(argv['news_doc'], 'news', 'news', isDebug=argv['debug'])
    error_record['parse_news'] = result
    '''
    result = run_java(isDebug=argv['debug'])
    error_record['run_java'] = result

    debugger.checkError(error_record)

def parse(docs_dir, dst, doc_type, isDebug=False):
    sys_tools = SystemTools(isDebug)
    f = open(dst, 'w')
    parser = xml.sax.make_parser()
    handler = DocContentHandler(f, doc_type)
    parser.setContentHandler(handler)
    funct = parser.parse
    sys_tools.osWalk(docs_dir, funct, error=xml.sax._exceptions.SAXParseException)
    f.close()
    return sys_tools.errorRecord

def run_java(isDebug=False):
    sys_tools = SystemTools(isDebug)
    sys_tools.customizedCMD('sh ./preprocessing/doc_pre/ja_code/run_seg_and_pos.sh')
    return sys_tools.errorRecord

